package org.gangcai.mac.shop.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;

import org.greenrobot.eventbus.EventBus;
import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.bean.CommonResonseBean;
import org.gangcai.mac.shop.bean.EventBusBaseBean1;
import org.gangcai.mac.shop.bean.PullAuthoBean;
import org.gangcai.mac.shop.constants.Constant;
import org.gangcai.mac.shop.oberver.CommonObserver;
import org.gangcai.mac.shop.utils.MatisseUtils;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AuthoICCardActivity extends BaseAuthoActivity {

    @BindView(R.id.icCardNumberEt)
    EditText icCardNumberEt;
    @BindView(R.id.nextStep)
    Button nextStep;
    @BindView(R.id.icCardIv)
    ImageView icCardIv;
    private File file;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        getAuthoData("getbank_ident").subscribe(new CommonObserver<PullAuthoBean>() {
            @Override
            public void onSuccess(PullAuthoBean value) {
                icCardNumberEt.setText(value.getInfo().getCard_number());
                Glide.with(AuthoICCardActivity.this).load(Constant.IMAGE_PATH + value.getInfo().getCard_img());
            }
        });

    }

    @Override
    protected String getTitleName() {
        return "银行卡";
    }

    @Override
    public int getcontentView() {
        return R.layout.activity_autho_iccard;
    }

    @OnClick({R.id.nextStep, R.id.icCardIv})
    public void onViewClicked(View v) {
        if (v.getId() == R.id.nextStep) {

            if (TextUtils.isEmpty(icCardNumberEt.getText().toString())) {
                ToastUtils.showShort("请输入您的银行卡号");
            } else if (icCardNumberEt.getText().toString().length() != 19) {
                ToastUtils.showShort("请正确输入您的银行卡号");
            } else if (file == null) {
                ToastUtils.showShort("请上传银行卡照片");
            } else {
                prompDialog.showLoading("请等待");
                HashMap<String, String> paramMap = new HashMap<>();
                paramMap.put("uid", SPUtils.getInstance().getString("uid"));
                paramMap.put("card_number", icCardNumberEt.getText().toString());

                HashMap<String, File> fileMap = new HashMap<>();
                fileMap.put("card_img", file);
                submitDataToServer("bankcard_ident", paramMap, fileMap).
                        subscribe(new CommonObserver<CommonResonseBean>(prompDialog) {
                            @Override
                            public void onSuccess(CommonResonseBean value) {
                                if (value.getCode() == Constant.CODE_SUCC) {
                                    showMsg();
                                    EventBus.getDefault().postSticky(new EventBusBaseBean1());
                                    finish();
                                }
                            }
                        });

            }
        } else if (v.getId() == R.id.icCardIv) {
            //选中图片
            MatisseUtils.pickImage(this, 1, 200);
        }


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 200 && data != null) {
            List<Uri> images = MatisseUtils.getImages(data);
            if (images != null && images.size() == 1) {
                Uri uri = images.get(0);
                file = MatisseUtils.getFile(uri, this);
                Glide.with(this).load(uri).into(icCardIv);
            }
        }
    }
}
